#coding=utf-8
import mysql_connect_henghe

def delete(phone,status):
    connect = mysql_connect_henghe.mysql_connect()[0]  # connect是由connect_mysql函数返回的第一个值
    cursor = connect.cursor()
    # SQL 查询语句
    sql = "SELECT id from gdgyun_community.community_user WHERE mobile =" + phone
    # 执行sql语句
    cursor.execute(sql)
    result = cursor.fetchall()
    connect.commit()  # 实例提交命令
    userid = str(result[0][0])
    if status == '0':  #0为合伙人
        sql_id = "SELECT id from gdgyun_community.community_purchase_apply WHERE purchaser_mobile =" + phone
        cursor.execute(sql_id)
        result = cursor.fetchall()
        connect.commit()  # 实例提交命令
        id = str(result[0][0])
        #删除语句
        delete_sql = [
        "DELETE FROM gdgyun_community.user_operation_log WHERE user_id=" + userid
        ,"DELETE FROM gdgyun_community.reward_record WHERE user_id="+userid
        ,"DELETE FROM gdgyun_community.article_comment_reply WHERE article_comment_id IN (SELECT id FROM gdgyun_community.article_comment WHERE user_id="+userid+")"
        ,"DELETE FROM gdgyun_community.article_comment WHERE user_id="+userid
        ,"DELETE FROM gdgyun_community.article_comment_reply WHERE user_id="+userid
        ,"DELETE FROM gdgyun_community.article_error_report WHERE user_id="+userid
        ,"DELETE FROM gdgyun_community.reward_blacklist WHERE user_id="+userid
        ,"DELETE FROM gdgyun_community.score_shop_addresses WHERE user_id="+userid
        ,"DELETE FROM gdgyun_community.score_shop_orders WHERE user_id="+userid
        ,"DELETE FROM gdgyun_community.system_notification WHERE category='SINGLE_USER' AND receivers="+phone
        ,"DELETE FROM gdgyun_community.community_payment_order WHERE community_purchase_apply_id = "+id
        ,"DELETE from gdgyun_community.community_purchase_apply WHERE purchaser_mobile ="+phone
        ,"DELETE FROM community_certification WHERE id="+userid
        ,"DELETE FROM activity_lottery_record WHERE user_id="+userid
        , "DELETE FROM gdgyun_community.community_user WHERE mobile=" + phone
        ]
        # #执行删除语句
        for i in range(0,13,1):
            delete_sql1 = delete_sql[i]
            print delete_sql1
            cursor.execute(delete_sql1)
            connect.commit()  # 实例提交命令
        return u'删除用户成功'
    if status == '1':  #为1时仅为成员身份
        # 删除语句
        delete_sql = [
            "DELETE FROM gdgyun_community.user_operation_log WHERE user_id=" + userid
            , "DELETE FROM gdgyun_community.reward_record WHERE user_id=" + userid
            ,"DELETE FROM gdgyun_community.article_comment_reply WHERE article_comment_id IN (SELECT id FROM gdgyun_community.article_comment WHERE user_id=" + userid + ")"
            , "DELETE FROM gdgyun_community.article_comment WHERE user_id=" + userid
            , "DELETE FROM gdgyun_community.article_comment_reply WHERE user_id=" + userid
            , "DELETE FROM gdgyun_community.article_error_report WHERE user_id=" + userid
            , "DELETE FROM gdgyun_community.reward_blacklist WHERE user_id=" + userid
            , "DELETE FROM gdgyun_community.score_shop_addresses WHERE user_id=" + userid
            , "DELETE FROM gdgyun_community.score_shop_orders WHERE user_id=" + userid
            , "DELETE FROM gdgyun_community.system_notification WHERE category='SINGLE_USER' AND receivers=" + phone
            , "DELETE FROM community_certification WHERE id=" + userid
            , "DELETE FROM activity_lottery_record WHERE user_id=" + userid
            , "DELETE FROM gdgyun_community.community_user WHERE mobile=" + phone
        ]
        # #执行删除语句
        for i in range(0, 11, 1):
            delete_sql1 = delete_sql[i]
            print delete_sql1
            cursor.execute(delete_sql1)
            connect.commit()  # 实例提交命令
        return u'删除用户成功'



if __name__ == '__main__':
    delete('13438825857','1')